<template>
  <div class="container">
    <h1>{{ id ? "编辑" : "创建" }}文章</h1>
    <el-form :model="model" @submit.native.prevent="save">
      <el-form-item label="所属标签">
        <el-select v-model="model.tags" value-key="_id" multiple>
          <el-option
            v-for="item in tags"
            :key="item._id"
            :label="item.name"
            :value="item._id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="标题">
        <el-input v-model="model.title"></el-input>
      </el-form-item>

      <el-form-item label="内容">
        <vue-editor
          v-model="model.body"
          useCustomImageHandler
          @image-added="handleImageAdded"
        ></vue-editor>
      </el-form-item>

      <el-form-item label="描述">
        <el-input v-model="model.description"></el-input>
      </el-form-item>

      <el-form-item label="标题图片">
        <el-upload
          class="avatar-uploader"
          :action="$http.defaults.baseURL + '/upload'"
          :show-file-list="false"
          :on-success="afterUpload"
        >
          <img
            v-if="model.titlePicture"
            :src="model.titlePicture"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" native-type="submit">{{
          id ? "保存" : "创建"
        }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { VueEditor } from "vue2-editor";
export default {
  props: {
    id: {},
  },
  components: {
    VueEditor,
  },
  data() {
    return {
      model: {
        titlePicture: "",
      },
      tags: [],
    };
  },
  methods: {
    async handleImageAdded(file, Editor, cursorLocation, resetUploader) {
      var formData = new FormData();
      formData.append("file", file);

      const res = await this.$http.post("upload", formData);
      Editor.insertEmbed(cursorLocation, "image", res.data.url);
      resetUploader();
    },
    async save() {
      // 添加文章
      if (!this.id) {
        let res = await this.$http.post("/articles", this.model);
        this.$message({
          type: "success",
          message: "创建文章成功",
        });
        this.$router.push("/articles/list");
        // 编辑文章
      } else {
        let res = await this.$http.put(`/articles/${this.id}`, this.model);
        console.log(res.data);
        this.$message({
          type: "success",
          message: "文章保存成功",
        });
        this.$router.push("/articles/list");
      }
    },
    // 获取编辑文章初始数据
    async fetch() {
      const res = await this.$http.get(`/articles/${this.id}`);
      this.model = res.data;
      // console.log(res.data);
    },
    async fetchTag() {
      const res = await this.$http.get(`/tags/${this.id}`);
      this.tags = res.data;
      console.log(res.data);
    },
    async fetchTags() {
      const res = await this.$http.get("tags");
      this.tags = res.data;
      // console.log(res.data);
    },
    afterUpload(res) {
      // console.log(res);
      this.model.titlePicture = res.url;
    },
  },
  created() {
    this.id && this.fetch();
    this.id && this.fetchTag();
    this.fetchTags();
  },
};
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>